var validation = function(item, params) {
    var that = this;
    jQuery(item).change(function() {
        if (params === null) { return; }
        var value = jQuery(item).val();
        var classes = jQuery(item).attr("class").split(" ");
        var validation = "";
        for (var i = 0; i < classes.length; i++) {
            if (classes[i].indexOf("validate_") === 0) {
                validation = classes[i];
            }
        }
        if (validation != "") {
            var validationFn = "";
            var tmp = validation.split("_");
            tmp.splice(0, 1);
            validationFn = tmp.join("_");
            var validResult = {};
            try {
                validResult = eval("that." + validationFn + "(item, value, params)");
            }
            finally {
            }
        }
    });

    var valid = function(field) {
        jQuery(field).removeClass("error");
/*
        jQuery("div#pictogram_" + field.id).removeClass("waiting");
        jQuery("div#pictogram_" + field.id).removeClass("invalid");
        jQuery("div#pictogram_" + field.id).addClass("valid");
*/
        return {
            field : field,
            valid : true
        };
    }

    var waiting = function(field) {
        jQuery("div#pictogram_" + field.id).removeClass("invalid");
        jQuery("div#pictogram_" + field.id).removeClass("valid");
        jQuery("div#pictogram_" + field.id).addClass("waiting");
        return {
            field : field,
            valid : true
        };
    }

    var invalid = function(field, msg, errorCode) {
        jQuery(field).addClass("error");
/*
        jQuery("div#pictogram_" + field.id).removeClass("valid");
        jQuery("div#pictogram_" + field.id).removeClass("waiting");
        jQuery("div#pictogram_" + field.id).addClass("invalid");
*/
        return {
            field : field,
            valid : false,
            msg : msg,
            errorCode : errorCode
        };
    }

    var lateSetItemValid = function(field) {
        valid(field);
    }

    var lateSetItemInvalid = function(field, msg) {
        invalid(field, msg);
    }

    this.required = function(item, value) {
        if (value == "") { return invalid(item, "You have to fill out required fields!"); }
        return valid(item);
    }

    this.email = function(item, value) {
    }
    
    this.username_notexists = function(item, value) {
        callbacks = {
            success : function(result) {
                if (!result.exists) {
                    lateSetItemValid(item);
                } else {
                    lateSetItemInvalid(item, "This username is already in use!");
                }
            },
            failure : function(msg) {
            }
        };

        callService("register", "userexists", {
            username : value
        }, callbacks);
        return waiting(item);
    }

    this.password = function(item, value, params) {
        if (value.length < 8) {
            return invalid(item, "Password length must be at least 8 characters");
        }
        return valid(item);
    }
    
    this.password_confirm = function(item, value, params) {
        if (jQuery("input[id='" + params.pwfield + "']").val() == value) { return valid(item); }
        return invalid(item, "Re-typed password must be the same as original password!");
    }
}
